package com.uvlab.sitespot.io;

import com.uvlab.sitespot.init.JxAppFoundation;
import com.uvlab.sitespot.service.JxAppService;
import com.uvlab.sitespot.service.fdb.JxAppFdbService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Jys on 2016/5/16 0016.
 */

// Just for test in order to test workstation
@CrossOrigin
@RestController
@RequestMapping(value = "/rest/file")
public class JxFileUploadController {
    @Resource(name = "svc")
    private JxAppService svc;
//    @RequestMapping(method = RequestMethod.GET)
//    public List<String> get()
//    {
//        File rootFolder = new File(JxAppFoundation.DataRoot);
//        List<String> fileNames = Arrays.stream(rootFolder.listFiles())
//                .map(f -> f.getName())
//                .collect(Collectors.toList());
//        return fileNames;
//    }


    @RequestMapping(method = RequestMethod.POST)
    public void upload(@RequestParam("name") String name,
                         @RequestParam("file") MultipartFile file) {
        svc.getAppFdbService().uploadFile(name, file);
    }

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<byte[]> download(@RequestParam(value = "filename")String filename,
                                           @RequestParam(value = "day")String day) throws IOException {
        ResponseEntity<byte[]> ret;
       // ret=svc.getAppFdbService().downloadFile(filename,day);
        return svc.getAppFdbService().downloadFile(filename, day);
    }
}

